package cn.jeeweb.web.modules.equipment.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.equipment.entity.EqsFixture;
import cn.jeeweb.web.modules.equipment.entity.EqsReplace;
import cn.jeeweb.web.modules.equipment.entity.EqsSparePart;
import cn.jeeweb.web.modules.equipment.service.IEqsFixtureService;
import cn.jeeweb.web.modules.equipment.service.IEqsReplaceService;
import cn.jeeweb.web.modules.equipment.service.IEqsSparePartService;
import cn.xjzn.framework_Core.ModuleActionType;
import cn.xjzn.framework_Core.ModuleServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eqs/replace")
@ViewPrefix("")
@Log(title = "eqs_replace")
public class EqsReplaceController {

    @Autowired
    private IEqsReplaceService eqsReplaceService;
    @Autowired
    private IEqsFixtureService eqsFixtureService;
    @Autowired
    private IEqsSparePartService eqsSparePartService;

    @PostMapping("save")
    @Log(logType = ModuleActionType.INSERT)
    public Response save(String originId, String[] exchangeIds) {
//        if (StringUtils.isNotBlank(originId)) {
//            EntityWrapper<EqsReplace> entityWrapper = new EntityWrapper<>(EqsReplace.class);
//            entityWrapper.eq("originId", originId);
//            eqsReplaceService.delete(entityWrapper);
//        } else {
//            return Response.error("数据格式有误");
//        }

        if (exchangeIds != null && exchangeIds.length > 0) {
            for (String exchangeId : exchangeIds) {
                EqsReplace eqsReplace = new EqsReplace();
                eqsReplace.setOriginId(originId);
                eqsReplace.setExchangeId(exchangeId);
                eqsReplaceService.insert(eqsReplace);
            }
        }
        return Response.ok("保存成功");
    }

    /**
     * 删除原有的与替代的关联
     * @param templateIds
     * @return
     */
    @RequestMapping(value = "batchDelete", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam(value = "id", required = false)String id,@RequestParam(value = "templateIds", required = false)String[] templateIds) {
        EntityWrapper<EqsReplace> entityWrapper = new EntityWrapper<>(EqsReplace.class);
        entityWrapper.eq("originId",id);
        entityWrapper.in("exchangeId",templateIds);
        eqsReplaceService.delete(entityWrapper);
        return Response.ok("删除成功");
    }

    @RequestMapping(value = "getEqsReplaces", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public Response getEqsEquipmentFixtures(String originId,String moduleId){
        Map<String, Object> map = new HashMap<String, Object>();
        if(ModuleServe.eqsSparePartModule.getValue().equals(moduleId)){
            List<EqsSparePart> eqsSpareParts = eqsSparePartService.getEqsSparePartByOriginId(originId);
            map.put("records",eqsSpareParts);
        }else if(ModuleServe.eqsFixtureModule.getValue().equals(moduleId)){
            List<EqsFixture> eqsFixtures = eqsFixtureService.getEqsFixtureByOriginId(originId);
            map.put("records",eqsFixtures);
        }
        return Response.ok("",map);
    }

}